package ace.cmp.spring.graphql.test.test.core.graphql;

import ace.cmp.spring.graphql.core.annotation.SchemaExtendMapping;
import ace.cmp.spring.graphql.test.test.core.model.Book;
import ace.cmp.spring.graphql.test.test.core.repository.AuthorRepository;
import ace.cmp.spring.graphql.test.test.core.repository.BookRepository;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author caspar
 * @date 2023/8/22 17:08
 */
@SchemaExtendMapping(fieldPrefix = "typePrefix", fieldSuffix = "typeSuffix")
@Slf4j
@RestController
public class SchemaExtendMappingBookController {

  private final BookRepository bookRepository;
  private final AuthorRepository authorRepository;
  private final BatchLoaderRegistry batchLoaderRegistry;

  public SchemaExtendMappingBookController(
      BookRepository bookRepository,
      AuthorRepository authorRepository,
      BatchLoaderRegistry batchLoaderRegistry) {
    this.bookRepository = bookRepository;
    this.authorRepository = authorRepository;
    this.batchLoaderRegistry = batchLoaderRegistry;
  }

  /**
   * 动态名
   *
   * @return
   */
  @SchemaExtendMapping(fieldPrefix = "fieldPrefix", fieldSuffix = "fieldSuffix")
  @QueryMapping("getDynamic")
  public CompletableFuture<Book> getBook(@Argument("id") Long id) {
    return CompletableFuture.supplyAsync(
        () -> {
          return this.bookRepository.getById(id);
        });
  }
}
